package com.server.modules.sys.utils.date;

import com.server.modules.sys.common.StaticDefine;
import org.nutz.lang.Strings;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/26.
 */
public class DateTimeUtil {
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    public static final String DEFAULT_CN_DATE_FORMAT = "yyyy年MM月dd日";

    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    public static final String DEFAULT_POIT_DATE_FORMAT = "yyyy.MM.dd";

    private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(DEFAULT_DATE_FORMAT);

    private static final SimpleDateFormat DATETIME_FORMAT = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);

    /**
     * @Title: parseStrToDate
     * @Description: TODO(根据格式将字符串转换为日期)
     * @param @param dateStr
     * @param @param formatStr
     * @param @return    设定文件
     * @return Date    返回类型
     * @throws
     */
    public static Date parseStrToDate(String dateStr, String formatStr) {
        if (Strings.isEmpty(dateStr) || Strings.isEmpty(formatStr)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(formatStr);
        Date date = null;
        try {
            date = dateFormat.parse(dateStr);
        } catch (ParseException ex) {
            date = null;
        }
        dateFormat = null;
        return date;
    }

    /**
     * @Title: formatDateToStr
     * @Description: TODO(日期转换为字符串)
     * @param @param date
     * @param @param formatStr
     * @param @return    设定文件
     * @return String    返回类型
     * @throws
     */
    public static String formatDateToStr(Date date, String formatStr) {
        if (date == null || Strings.isEmpty(formatStr)) {
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        String dateStr = format.format(date);
        format = null;
        return dateStr;
    }

    /**
     * @Title: getNow
     * @Description: TODO(获取系统当前时间)
     * @param @return    设定文件
     * @return Date    返回类型
     * @throws
     */
    public static Date getNow() {
        Calendar now = Calendar.getInstance();
        return now.getTime();
    }

    /**
     * @Title: getPeriodByDate
     * @Description: TODO(获取时间区间)
     * @param date 选择的日期
     * @param type 区间类型
     * @return Date    返回类型
     * @throws
     */
    public static Map<String, Date> getPeriodByDate(Date date, String type) {
        Map<String, Date> map = new HashMap<String, Date>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        switch(type){
            case StaticDefine.DIC_PERIOD_TYPE.DAY:
                map.put("start", calendar.getTime());
                calendar.add(Calendar.DATE, 1);
                map.put("end", calendar.getTime());
                break;

            case StaticDefine.DIC_PERIOD_TYPE.WEEK:

                calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); //获取本周一的日期
                map.put("start", calendar.getTime());
                //获取上周日
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
                //增加一个星期，才是我们中国人理解的本周日的日期
                calendar.add(Calendar.WEEK_OF_YEAR, 1);
                calendar.add(Calendar.DATE, 1);
                map.put("end", calendar.getTime());
                break;

            case StaticDefine.DIC_PERIOD_TYPE.MONTH:
                calendar.set(Calendar.DATE, 1);
                map.put("start", calendar.getTime());
                calendar.add(Calendar.MONTH, 1);
                map.put("end", calendar.getTime());
                break;

            default:
                break;
        }

        return map;
    }

    public static int getMonthSpace(Date start, Date end) {
        if (start.after(end)) {
            Date t = start;
            start = end;
            end = t;
        }
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(start);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);

        int year = endCalendar.get(Calendar.YEAR)
                - startCalendar.get(Calendar.YEAR);
        int month = endCalendar.get(Calendar.MONTH)
                - startCalendar.get(Calendar.MONTH);

        return year * 12 + month;
    }
}
